package c.main.controller;

import c.main.core.*;
import c.main.domain.LoginUser;
import c.main.jdbc.JdbcManager;
import c.main.jdbc.JdbcTemplate;
import c.main.service.SysService;
import c.main.util.RequestMethod;
import c.main.util.StringUtils;
import c.main.util.WebUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

/**
 * Author: chengwei
 * Date:   2016/1/14
 * Time:   17:44
 */
public abstract class BaseController extends HttpServlet {
    
    private Map<String,String> actionMap = new HashMap<>();
    private Map<String,RequestMethod> methodMap = new HashMap<>();
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doService(req,resp,true);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doService(req,resp, false);
    }

    //post get 统一处理
    private void doService(HttpServletRequest req, HttpServletResponse resp, boolean isPost) {
        String action = req.getParameter(WebUtils.ACTION_NAME);
        String methodName;
        if (StringUtils.isNotEmpty(action) && isPost && RequestMethod.POST.equals(methodMap.get(action))
                    || !isPost && RequestMethod.GET.equals(methodMap.get(action)) 
                    || RequestMethod.ANY.equals(methodMap.get(action))) {
                methodName = actionMap.get(action);
        } else {
            methodName = isPost?"doDefaultPost":"doDefaultGet";
        }
        try {
            //获取方法
            Method method = this.getClass().getMethod(methodName, HttpView.class);
            HttpView view = new HttpView(req,resp);
            method.invoke(this,view);
            
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
            throw new RuntimeException(String.format("Bean[%s]方法[%s]未定义",this.getClass().getName(),methodName));
        } catch (ReflectiveOperationException e) {
            e.printStackTrace();
        } 
    }

    @Override
    public void init() throws ServletException {
        super.init();
        Field[] fields = this.getClass().getDeclaredFields();
        WcsResource wcsResource;
        Object o;
        for (Field field : fields) {
            wcsResource = field.getAnnotation(WcsResource.class);
            if (wcsResource != null) {
                o = C.getBean(field.getType());
                try {
                    field.setAccessible(true);
                    field.set(this,o);
                } catch (IllegalAccessException e) {
                    throw new RuntimeException(String.format("Bean[%s]无法注入",o));
                }
            }
        }

        WebRequest webRequest;
        Method[] methods = this.getClass().getDeclaredMethods();
        for (Method method : methods) {
            webRequest = method.getAnnotation(WebRequest.class);
            if (webRequest != null) {
                actionMap.put(webRequest.act(),method.getName());
                methodMap.put(webRequest.act(),webRequest.method());
            }
        }
    }

    @Override
    public void destroy() {
        super.destroy();
    }
    
    public abstract void doDefaultGet (HttpView view) throws ServletException, IOException;

    public void doDefaultPost (HttpView view) throws ServletException, IOException {
        doDefaultGet(view);
    }
}
